package com.tcgroup.phone.base_core.adapter;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;

import com.tcgroup.phone.base_core.framework.mvvm.fragment.BaseMvvmFragment;

import java.util.ArrayList;

/**
 * 作者: wangfei
 * 创建时期: 2020/4/20.
 * 包名: com.tcgroup.phone.base_core.adapter
 * 类名: OSAndroidMvvm
 */
public abstract class AbstractVpFragmentAdapter<T extends BaseMvvmFragment> extends FragmentStatePagerAdapter {

    protected ArrayList<T> mFragments = new ArrayList<>();


    public AbstractVpFragmentAdapter(@NonNull FragmentManager fm, int behavior) {
        super(fm, behavior);
    }

    public AbstractVpFragmentAdapter(@NonNull FragmentManager fm, ArrayList<T> mFragments) {
        super(fm);
        this.mFragments = mFragments;
    }

    @NonNull
    @Override
    public Fragment getItem(int position) {
        return getFragment(position);
    }

    @Override
    public int getCount() {
        return mFragments.size();
    }


    public void addAllFragments(ArrayList<T> fragments){
        mFragments.addAll(fragments);
        notifyDataSetChanged();
    }

    public void removeAllFragment(ArrayList<? extends BaseMvvmFragment> fragments){
        mFragments.removeAll(fragments);
        notifyDataSetChanged();
    }

    public void addFragment(T fragment){
        mFragments.add(fragment);
        notifyDataSetChanged();
    }

    public void removeFragment(T fragment){
        mFragments.remove(fragment);
        notifyDataSetChanged();
    }

    public void clear(){
        mFragments.clear();
        notifyDataSetChanged();
    }

    protected abstract BaseMvvmFragment getFragment(int position);

}
